import { _decorator, Collider, Collider2D, v2, v3, Vec2, Vec3 } from 'cc';
import { Unit } from './Unit';
import { ISpawnerEventCall, SpawnEvent } from './Component/SpawnerCom';
import { UnitPlanet } from './UnitPlanet';
import { Bullet } from './Bullet';
import AssetsMgr from '../Model/AssetsMgr';
import { UnitSpacecraftBase } from './UnitSpacecraftBase';
import { UnitType } from '../Util/Global';
const { ccclass, property } = _decorator;

@ccclass('UnitAssault')
export class UnitAssault extends UnitSpacecraftBase implements ISpawnerEventCall {

    // protected bullet_children: Array<Unit> = Array();

    AddTarget(t: Unit) {
        super.AddTarget(t);
        if (this.parentUnit)
            this.parentUnit.AddEnemy(t);
    }

    LostTarget(t: Unit) {
        super.LostTarget(t);
        if (this.parentUnit)
            this.parentUnit.LostEnemy(t);
    }

    protected startLogic(): void {
        this.unitType = UnitType.UnitAssault;
        // this.canPlanetHit = true;

        this.arsenal.AddProduct(AssetsMgr.getUnitPrefab(UnitType.Bullet));
        this.arsenal.onSpawnEventCallback('UnitAssaultHandle', this);
    }

    protected onDisable(): void {
        super.onDisable();
        this._changeCount = 0;
    }

    protected _changeMoveInterval: number = 1;
    protected _changeCount: number = 0;

    protected updateLogic(dt: number) {
        this.TimeChangeMoveDir();

        let tooFar = this.TooFarFromParent();
        if (this.target && !this.target.death) {
            let chaseTarget = false;
            if (this.target.unitType == UnitType.UnitPlanet) {
                // if (!this.target.getComponent(FactoryCom)) {
                //     this.target = null;
                //     return;
                // }
                if (this.arsenal && this.arsenal.StartIdleWorkShop() > 0) {//发射子弹成功
                    chaseTarget = true;
                }
            }
            else {
                this.arsenal && this.arsenal.StartIdleWorkShop();
                chaseTarget = true;
            }

            if (chaseTarget) {
                this.ChangeChaseTargetDir();
            }
        }
        else if (!tooFar) {
            this.target = this.FindTargetWeightAndNearest();
            if (!this.target || this.target.death) { //从父级获取目标
                if (this.parentUnit) {

                    this.target = (<UnitPlanet>this.parentUnit).FindEnemyNearest();
                }
            }
            if (this.target == this) {
                console.log('警告==>目标是自己！！？');
            }
        }
        else {
            this.ChangeMoveDir(true);
        }

        //太远且已经丢失视野
        if (tooFar && this.checkEnemyObservedFrequencyInParent(this.target) <= 0) {
            this.target = null;
        }

        this.AutoMove();
    }

    protected checkEnemyObservedFrequencyInParent(enemy: Unit) {
        if (this.parentUnit) {
            return (<UnitPlanet>this.parentUnit).GetEnemyObservedFrequency(enemy)
        }

        return 0;
    }

    TimeChangeMoveDir(back?: boolean) {

        if (this._lifeTime > this._changeMoveInterval * this._changeCount) {
            this.ChangeMoveDir(back);
            this._changeCount++;
        }
    }

    SpawnEventCallback(unit: Unit, event: SpawnEvent) {
        if (unit && event == SpawnEvent.SPAWNING) {

            // let offset = Vec3.ZERO.clone();
            // this.GetRotatedPos(offset);
            // offset.add(this.unitWorldPos);
            this.onBulletCreate(unit, this.unitWorldPos);
        }

        return true;
    };

    protected onBulletCreate(unit: Unit, spawningPos?: Vec3) {
        let bullet = <Bullet>unit;

        let enemy = this.target;
        if (enemy && !enemy.death) {

            bullet.parentUnit = this.parentUnit;//设置工厂作为父级
            bullet.SetTarget(enemy);
            bullet.node.setWorldPosition(spawningPos)
        }
    }

    //trigger是在bullet组 永远不会和子弹触发
    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        super.onTriggerEnter(selfCollider, otherCollider);
        // 只在两个碰撞体开始接触时被调用一次
        let other = otherCollider.node.getComponent(Unit);

        if (this.IsEnemy(other)) {//对方和自己不来自一个父级

            this.AddTarget(other);

            this.target = this.FindTargetWeightAndNearest(other);
            if (this.target == this) {
                console.log('警告==>目标是自己！！？', other == this.target);
            }
            // if (this.target == null) {
            //     console.log('警告==>刚刚发现敌人但是忽略了！！？', other);
            // }
        }
    }

    protected onTriggerExit(selfCollider: Collider2D, otherCollider: Collider2D) {
        super.onTriggerExit(selfCollider, otherCollider);
        let other = otherCollider.node.getComponent(Unit);

        if (this.IsEnemy(other)) {
            this.LostTarget(other);
        }
    }

}


